package com.scalar.chiptrack.barcode;

import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.utils.FileUtils;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.krysalis.barcode4j.BarcodeException;
import org.krysalis.barcode4j.BarcodeGenerator;
import org.krysalis.barcode4j.BarcodeUtil;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.xml.sax.SAXException;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

/**
 * User: ssujan
 * Date: Apr 28, 2010
 * Time: 10:27:46 PM
 */
public class BarcodeGeneratorUtil {
    protected final Log logger = LogFactory.getLog(getClass());
    private static BarcodeGenerator barcodeGenerator;
    public static final String IMAGE_MIME_TYPE_PNG = "image/x-png"; 
    public static final String IMAGE_MIME_TYPE_GIF = "image/gif";
    public static final String IMAGE_MIME_TYPE_JPG = "image/jpeg";

    private BarcodeGeneratorUtil() {
    }

    public static BarcodeGenerator getBarcodeGenerator() throws ConfigurationException, IOException, SAXException, BarcodeException {
        if( barcodeGenerator == null ) {
            DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
            Configuration cfg = builder.buildFromFile(new File(ConfigManager.getBarcodeConfigurationFile()));
            barcodeGenerator = BarcodeUtil.getInstance().createBarcodeGenerator( cfg );
        }

        return barcodeGenerator;
    }

    /**
     *
     * @param message for which the barcode to be generated
     * @param imageMimeType image format to be used like "image/x-png" for "png".
     * @return the absolute generated barcode image file path
     * @throws IOException
     * @throws ConfigurationException
     * @throws SAXException
     * @throws BarcodeException
     * @see #IMAGE_MIME_TYPE_GIF
     * @see #IMAGE_MIME_TYPE_PNG
     * @see #IMAGE_MIME_TYPE_GIF
     */
    public static String generateBarcodeImg(String message, String imageMimeType) throws IOException, ConfigurationException, SAXException, BarcodeException {
        String filepath = ConfigManager.getMailerTempDir() + File.separator + FileUtils.normalizeFileName(message.substring(0,message.length()>50?50:message.length())+"_"+System.currentTimeMillis()) +"."+getFileExtn(imageMimeType);
        OutputStream out = new java.io.FileOutputStream(filepath);
        BitmapCanvasProvider provider = new BitmapCanvasProvider(out, imageMimeType, 300, BufferedImage.TYPE_BYTE_GRAY, true, 0);
        getBarcodeGenerator().generateBarcode(provider, message);
        provider.finish();
        return filepath;
    }

    /**
     * generated a png barcode image.
     * @param message for which the barcode to be generated
     * @return the absolute generated barcode image file path
     * @throws IOException
     * @throws ConfigurationException
     * @throws SAXException
     * @throws BarcodeException
     */
    public static String generateBarcodeImg(String message) throws IOException, ConfigurationException, SAXException, BarcodeException {
        return generateBarcodeImg(message, BarcodeGeneratorUtil.IMAGE_MIME_TYPE_PNG);
    }

    private static String getFileExtn(String imageMimeType) {
        if(IMAGE_MIME_TYPE_PNG.equalsIgnoreCase(imageMimeType)) {
            return "png";
        } else if(IMAGE_MIME_TYPE_GIF.equalsIgnoreCase(imageMimeType)) {
            return "gif";
        } else if(IMAGE_MIME_TYPE_JPG.equalsIgnoreCase(imageMimeType)) {
            return "jpg";
        }
        return "img";
    }

    public static void main(String args[]) throws Exception {
        System.out.println("BARCODE=" + BarcodeGeneratorUtil.generateBarcodeImg("SIT8102AI-TU-2T")); //STARTING PART NO
        System.out.println("BARCODE=" + BarcodeGeneratorUtil.generateBarcodeImg("SIT8102AI-22-33E-25.00000")); //FINAL PART NO
        System.out.println("BARCODE=" + BarcodeGeneratorUtil.generateBarcodeImg("BI722AE00250000002.txt"));  //JEDEC FILE NAME
        System.out.println("BARCODE=" + BarcodeGeneratorUtil.generateBarcodeImg("01/26/2010")); //DATE
        System.out.println("BARCODE=" + BarcodeGeneratorUtil.generateBarcodeImg("0099")); //LOT NO
        System.out.println("BARCODE=" + BarcodeGeneratorUtil.generateBarcodeImg("A8002-1"));  //MKTG PART NO
        System.out.println("BARCODE=" + BarcodeGeneratorUtil.generateBarcodeImg("GW9651.1"));  //LOT NO
    }
}
